package com.springboot.mongo.dao;

import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import com.springboot.mongo.entity.MongoEntity;
import java.util.List;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

/**
 * 统一公共mongodb dao接口
 *
 * @author supu
 * @date 2019-08-26 14:12
 **/
public class GerericMongoDAO<T extends MongoEntity<E>, E> {
    protected MongoOperations mongoOperations;

    public GerericMongoDAO() {
    }

    public GerericMongoDAO(MongoOperations mongoOperations) {
        this.mongoOperations = mongoOperations;
    }

    public void setMongoOperations(MongoOperations mongoOperations) {
        this.mongoOperations = mongoOperations;
    }

    public MongoOperations getMongoOperations() {
        return mongoOperations;
    }

    /**
     * 插入对象列表
     *
     * @param entity
     * @return
     */
    public void insertAllMongoEntity(List<T> entity) {
        mongoOperations.insertAll(entity);
    }

    /**
     * 插入对象
     *
     * @param entity
     * @return
     */
    public E insertMongoEntity(T entity) {
        mongoOperations.insert(entity);
        return entity.getPrimaryKey();
    }

    /**
     * 保存，有则更新无则新增
     *
     * @param entity
     * @return
     */
    public E saveMongoEntity(T entity) {
        mongoOperations.save(entity);
        return entity.getPrimaryKey();
    }

    /**
     * 保存，有则更新无则新增
     *
     * @param entity
     * @param collectionName
     * @return
     */
    public E saveMongoEntity(T entity, String collectionName) {
        mongoOperations.save(entity, collectionName);
        return entity.getPrimaryKey();
    }

    /**
     * 更新匹配到的第一条数据
     *
     * @param query
     * @param update
     * @param entityClass
     * @return
     */
    public UpdateResult updateFirst(Query query, Update update, Class<T> entityClass) {
        return mongoOperations.updateFirst(query, update, entityClass);
    }

    /**
     * 批量更新数据
     *
     * @param query
     * @param update
     * @param entityClass
     * @return
     */
    public UpdateResult updateMulti(Query query, Update update, Class<T> entityClass) {
        return mongoOperations.updateMulti(query, update, entityClass);
    }

    /**
     * 根据主键查询
     *
     * @param id
     * @param entityClass
     * @return
     */
    public T findMongoEntityById(String id, Class<T> entityClass) {
        return mongoOperations.findById(id, entityClass);
    }

    /**
     * 根据条件查询列表
     *
     * @param query
     * @param entityClass
     * @return
     */
    public List<T> query(Query query, Class<T> entityClass) {
        return mongoOperations.find(query, entityClass);
    }

    /**
     * 根据条件查询匹配到第一条数据(添加时间最早的记录)
     *
     * @param query
     * @param entityClass
     * @return
     */
    public T queryOne(Query query, Class<T> entityClass) {
        return mongoOperations.findOne(query, entityClass);
    }

    /**
     * 分组查询
     *
     * @param aggregation
     * @param outputType
     * @param <O>
     * @return
     */
    public <O> AggregationResults<O> aggregate(TypedAggregation<?> aggregation, Class<O> outputType) {
        return mongoOperations.aggregate(aggregation, outputType);
    }

    /**
     * 删除对象
     *
     * @param query
     * @param entityClass
     * @return
     */
    public DeleteResult remove(Query query, Class<T> entityClass) {
        return mongoOperations.remove(query, entityClass);
    }
}
